/**
 * 初始化ContestRule详情对话框
 */
var ContestRuleInfoDlg = {
    contestRuleInfoData : {}
};

/**
 * 清除数据
 */
ContestRuleInfoDlg.clearData = function() {
    this.contestRuleInfoData = {};
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ContestRuleInfoDlg.set = function(key, val) {
    this.contestRuleInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ContestRuleInfoDlg.get = function(key) {
    return $("#" + key).val();
};

/**
 * 关闭此对话框
 */
ContestRuleInfoDlg.close = function() {
    parent.layer.close(window.parent.ContestRule.layerIndex);
};

/**
 * 收集数据
 */
ContestRuleInfoDlg.collectData = function() {
    this
    .set('id')
    .set('companyLevel')
    .set('companyLevelCode')
    .set('contestType')
    .set('contestTypeCode')
    .set('winType')
    .set('winTypeCode')
    .set('integral')
    ;
};

/**
 * 提交添加
 */
ContestRuleInfoDlg.addSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/contestRule/add", function(data){
        Feng.success("添加成功!");
        window.parent.ContestRule.table.refresh();
        ContestRuleInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.contestRuleInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
ContestRuleInfoDlg.editSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/contestRule/update", function(data){
        Feng.success("修改成功!");
        window.parent.ContestRule.table.refresh();
        ContestRuleInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.contestRuleInfoData);
    ajax.start();
};

/**
 * 选择框改变时
 */
ContestRuleInfoDlg.companyLevelChange = function () {
    $('#companyLevelCode').change(function(){
        var companyLevel = $("#companyLevelCode option:selected").text();
        $("#companyLevel").val(companyLevel);
    });
};

ContestRuleInfoDlg.contestTypeChange = function () {
    $('#contestTypeCode').change(function(){
        var contestType = $("#contestTypeCode option:selected").text();
        $("#contestType").val(contestType);
    });
};

ContestRuleInfoDlg.winTypeChange = function () {
    $('#winTypeCode').change(function(){
        var winType = $("#winTypeCode option:selected").text();
        $("#winType").val(winType);
    });
};

/**
 * 上传提交
 */
ContestRuleInfoDlg.upload = function () {
    layui.use('upload', function(){
        var upload = layui.upload;
        //固定格式导入
        upload.render({
            elem: '#selectFile'
            ,url: '/ContestRule/import'
            ,auto: false
            ,accept: "file"
            ,exts: "xml|xlsx"
            ,bindAction: '#fixImport'
            ,before: function () {
                layer.msg("正在读取文件，请稍后...")
                loadIndex = layer.load(2);
            }
            ,done: function(res){
                layer.close(loadIndex);
                if(res.ok){
                    layer.msg(res.ok);
                }else{
                    layer.msg(res.fail);
                }

            }
        });

    });
};

/**
 * 提交校验
 */
ContestRuleInfoDlg.formValidator = function () {

    $('.form-horizontal').bootstrapValidator({
        message: '校验失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '员工姓名不能为空'
                    }
                }
            }
        }
    });
};

/**
 * 选择员工
 */
ContestRuleInfoDlg.selectEmp = function () {
    layer.open({
        type: 2,
        area: ['800px', '500px'],
        fixed: false, //不固定
        maxmin: true,
        content: '../employee/select_list',
        btn: ['确定'],
        yes: function(index, layero){
            var res = window["layui-layer-iframe" + index].Employee.callbackdata();
            $("#empName").val(res.empName);
            $("#empCode").val(res.empCode);
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }
    });
};


$(function() {
    ContestRuleInfoDlg.formValidator();
    ContestRuleInfoDlg.upload();
    ContestRuleInfoDlg.companyLevelChange();
    ContestRuleInfoDlg.contestTypeChange();
    ContestRuleInfoDlg.winTypeChange();
});
